<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        clearable
        v-model="queryForm.provinceId"
        class="filter-item"
        filterable
        placeholder="省份"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseProvince', 'query')"
      >
        <el-option
          v-for="(item, index) in provinces"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.cityId"
        class="filter-item"
        filterable
        placeholder="城市"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseCity', 'query')"
      >
        <el-option
          v-for="(item, index) in citys"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.districtId"
        class="filter-item"
        filterable
        placeholder="区"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseRegin', 'query')"
      >
        <el-option
          v-for="(item, index) in districts"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        v-model="queryForm.communityId"
        class="filter-item"
        filterable
        placeholder="小区"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseCommunity', 'query')"
      >
        <el-option
          v-for="(item, index) in communitys"
          :key="index"
          :value="item.rowId"
          :label="item.name"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        @click="openCreateDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="regionName"
        label="区域名"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="regionCode"
        label="区域编号"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="communityName"
        label="小区名称"
        width="450"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="lng" label="经度" width="200" align="center">
      </el-table-column>
      <el-table-column prop="lat" label="纬度" width="200" align="center">
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态" width="150">
        <template slot-scope="scope">
          {{ Dicts.CONSUMER_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        width="250"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            @click="editExample(scope.row)"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            title="编辑"
          ></el-button>
          <el-button
            @click="handleExample(scope.row.rowId)"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`${exampleForm.rowId ? '新增' : '编辑'}楼栋`"
      :visible.sync="dialogFormVisible"
      width="50%"
      :close-on-click-modal="false"
    >
      <el-form
        :model="exampleForm"
        label-width="110px"
        :rules="exampleFormRules"
        ref="exampleForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="省份" prop="provinceId">
              <el-select
                clearable
                v-model="exampleForm.provinceId"
                filterable
                placeholder="省份"
                style="width: 100%;"
                @change="value => cascade(value, 'chooseProvince', 'example')"
              >
                <el-option
                  v-for="(item, index) in provinces"
                  :key="index"
                  :value="item.rowId"
                  :label="item.cnName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域名" prop="regionName">
              <el-input
                v-model.trim="exampleForm.regionName"
                auto-complete="off"
                maxlength="20"
                placeholder="请输入区域名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="城市" prop="cityId">
              <el-select
                clearable
                v-model="exampleForm.cityId"
                filterable
                placeholder="城市"
                style="width: 100%;"
                @change="value => cascade(value, 'chooseCity', 'example')"
              >
                <el-option
                  v-for="(item, index) in citys"
                  :key="index"
                  :value="item.rowId"
                  :label="item.cnName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域编号" prop="regionCode">
              <el-input
                v-model.trim="exampleForm.regionCode"
                auto-complete="off"
                maxlength="20"
                placeholder="请输入区域编号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区县" prop="districtId">
              <el-select
                clearable
                v-model="exampleForm.districtId"
                filterable
                placeholder="区县"
                style="width: 100%;"
                @change="value => cascade(value, 'chooseDistrict', 'example')"
              >
                <el-option
                  v-for="(item, index) in districts"
                  :key="index"
                  :value="item.rowId"
                  :label="item.cnName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select
                placeholder="可选"
                style="width: 100%;"
                v-model="exampleForm.status"
              >
                <el-option
                  v-for="(val, key) in Dicts.CONSUMER_STATUS"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区" prop="communityId">
              <el-select
                v-model="exampleForm.communityId"
                filterable
                placeholder="小区"
                style="width: 100%;"
                @change="value => cascade(value, 'chooseCommunity', 'example')"
              >
                <el-option
                  v-for="(item, index) in communitys"
                  :key="index"
                  :value="item.rowId"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="经度" prop="lng">
              <el-input
                v-model="exampleForm.lng"
                auto-complete="off"
                maxlength="20"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="纬度" prop="lat">
              <el-input
                v-model="exampleForm.lat"
                auto-complete="off"
                maxlength="20"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveExampleForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPersonDistributionControl,
  savePersonDistributionControl,
  deletePersonDistributionControl
} from '@/api/consumer'
import { queryProvince, getCity, getDistrict } from '@/api/area'
import { selectAllCommunity } from '@/api/community'
export default {
  name: 'Consumer',
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      exampleForm: {
        rowId: undefined
      },
      modifyForm: {},
      exampleFormRules: {
        provinceId: [
          { required: true, message: '请选择省份', trigger: 'blur' }
        ],
        cityId: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        districtId: [
          { required: true, message: '请输入选择区域', trigger: 'blur' }
        ],
        communityId: [
          { required: true, message: '请选择小区', trigger: 'blur' }
        ],
        regionName: [
          { required: true, message: '请输入区域名', trigger: 'blur' }
        ],
        regionCode: [
          { required: true, message: '请输入区域编号', trigger: 'blur' }
        ],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      attributeOptions: Dicts.USER_ATTRIBUTE,
      statusOptions: Dicts.CONSUMER_STATUS,
      records: [],
      provinces: [],
      citys: [],
      districts: [],
      communitys: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryPersonDistributionControl(this.queryForm).then(
        ({ data: { data } }) => {
          this.queryForm.current = data.current
          this.queryForm.pages = data.pages
          this.queryForm.total = data.total
          this.queryForm.size = data.size
          this.records = data.records
          this.loading = false
        }
      )
      queryProvince().then(({ data: { data } }) => {
        this.provinces = data
        this.loading = false
      })
    },
    openCreateDialog() {
      this.exampleForm = []
      this.dialogFormVisible = true
      this.$refs.exampleForm && this.$refs.exampleForm.resetFields()
    },
    saveExampleForm() {
      this.$refs.exampleForm.validate(valid => {
        if (valid) {
          savePersonDistributionControl(this.exampleForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    editExample({
      rowId,
      regionName,
      regionCode,
      communityId,
      lng,
      lat,
      provinceId,
      cityId,
      districtId,
      townId,
      villageId,
      status
    }) {
      this.openCreateDialog()
      this.exampleForm = {
        rowId: rowId,
        regionCode: regionCode,
        regionName: regionName,
        communityId: communityId,
        lat: lat,
        lng: lng,
        status: status,
        provinceId: provinceId,
        cityId: cityId,
        districtId: districtId,
        townId: townId,
        villageId: villageId
      }
      queryProvince().then(({ data: { data } }) => {
        this.provinces = data
        this.loading = false
      })
      getCity(this.exampleForm.provinceId).then(({ data: { data } }) => {
        this.citys = data
        this.loading = false
      })
      getDistrict(this.exampleForm.cityId).then(({ data: { data } }) => {
        this.districts = data
        this.loading = false
      })
      selectAllCommunity(this.exampleForm.districtId).then(
        ({ data: { data } }) => {
          this.communitys = data
          this.loading = false
        }
      )
    },
    handleExample(rowId) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePersonDistributionControl(rowId).then(
          ({ data: { data, msg } }) => {
            this.doQuery()
            this.$message({
              showClose: true,
              message: msg,
              type: 'success'
            })
          }
        )
      })
    },
    cascade(rowId, level, param) {
      if (param === 'query') {
        if (rowId !== '') {
          switch (level) {
            case 'chooseProvince':
              getCity(rowId).then(({ data: { data } }) => {
                this.queryForm.cityId = ''
                this.queryForm.districtId = ''
                this.queryForm.communityId = ''
                this.citys = []
                this.districts = []
                this.communitys = []
                this.citys = data
              })
              return
            case 'chooseCity':
              getDistrict(rowId).then(({ data: { data } }) => {
                this.queryForm.districtId = ''
                this.queryForm.communityId = ''
                this.districts = []
                this.communitys = []
                this.districts = data
              })
              return
            case 'chooseRegin':
              selectAllCommunity(rowId).then(({ data: { data } }) => {
                this.queryForm.communityId = ''
                this.communitys = []
                this.communitys = data
              })
              return
            case 'chooseCommunity':
              selectAllCommunity(this.queryForm.districtId).then(
                ({ data: { data } }) => {
                  this.communitys = data
                }
              )
              return
          }
        } else {
          switch (level) {
            case 'chooseProvince':
              this.queryForm.cityId = ''
              this.queryForm.districtId = ''
              this.queryForm.communityId = ''
              this.citys = []
              this.districts = []
              this.communitys = []
              return
            case 'chooseCity':
              this.queryForm.districtId = ''
              this.queryForm.communityId = ''
              this.districts = []
              this.communitys = []
              return
            case 'chooseRegin':
              this.queryForm.communityId = ''
              this.communitys = []
              return
            case 'chooseCommunity':
              this.queryForm.communityId = ''
              return
          }
        }
      } else if (param === 'example') {
        if (rowId !== '') {
          switch (level) {
            case 'chooseProvince':
              getCity(rowId).then(({ data: { data } }) => {
                this.exampleForm.cityId = ''
                this.exampleForm.districtId = ''
                this.exampleForm.communityId = ''
                this.exampleForm.lng = ''
                this.exampleForm.lat = ''
                this.citys = []
                this.districts = []
                this.communitys = []
                const datas = this.provinces.find(item => item.rowId === rowId)
                this.exampleForm.provinceName = datas.cnName
                this.citys = data
              })
              return
            case 'chooseCity':
              getDistrict(rowId).then(({ data: { data } }) => {
                this.exampleForm.districtId = ''
                this.exampleForm.communityId = ''
                this.exampleForm.lng = ''
                this.exampleForm.lat = ''
                this.districts = []
                this.communitys = []
                const datas = this.citys.find(item => item.rowId === rowId)
                this.exampleForm.cityName = datas.cnName
                this.districts = data
              })
              return
            case 'chooseDistrict':
              selectAllCommunity(rowId).then(({ data: { data } }) => {
                this.exampleForm.communityId = ''
                this.exampleForm.lng = ''
                this.exampleForm.lat = ''
                this.communitys = []
                this.communitys = data
              })
              return
            case 'chooseCommunity':
              selectAllCommunity(this.exampleForm.districtId).then(
                ({ data: { data } }) => {
                  const datas = this.communitys.find(
                    item => item.rowId === rowId
                  )
                  this.exampleForm.lng = datas.lng
                  this.exampleForm.lat = datas.lat
                  this.communitys = data
                }
              )
              return
          }
        } else {
          switch (level) {
            case 'chooseProvince':
              this.exampleForm.cityId = ''
              this.exampleForm.districtId = ''
              this.exampleForm.communityId = ''
              this.citys = []
              this.districts = []
              this.communitys = []
              return
            case 'chooseCity':
              this.exampleForm.districtId = ''
              this.exampleForm.communityId = ''
              this.districts = []
              this.communitys = []
              return
            case 'chooseDistrict':
              this.exampleForm.communityId = ''
              this.communitys = []
              return
          }
        }
      }
    }
  },
  components: {}
}
</script>

<style scoped></style>
